package com.example.wangning.mycarofcountry.findcar.seniority;

import android.app.Activity;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.volley.AuthFailureError;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.example.mycarofcountry.entity.dao.*;
import com.example.wangning.mycarofcountry.R;
import com.example.wangning.mycarofcountry.cache.VolleySingleton;
import com.example.wangning.mycarofcountry.db.DaoHelper;
import com.example.wangning.mycarofcountry.tool.DividerItemDecoration;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dllo on 16/1/14.
 */
public class SeniorityFragment extends Fragment {
    private RecyclerView recyclerView;
    private SeniorityAdapter seniorityAdapter;
    private VolleySingleton volleySingleton;
    private DaoHelper helper;
    private String url = "http://app.api.autohome.com.cn/autov5.0.0/dealer/pdspecs-pm2-pi0-c110100-o0-b0-ss0-sp0-p1-s20-l0-minp0-maxp0-lon121.551115-lat38.88985.json";
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_senority,container,false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        recyclerView = (RecyclerView) view.findViewById(R.id.recycler_senority);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        seniorityAdapter = new SeniorityAdapter();
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST));
        recyclerView.setAdapter(seniorityAdapter);
        helper = DaoHelper.getInstance();
        getData(url);
    }


    private void getData(String url) {
        volleySingleton = VolleySingleton.getVolleyIntance();
        StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                ReduceCar reduceCar = gson.fromJson(response,ReduceCar.class);
                if (reduceCar != null){
                    ReduceResult reduceResult = reduceCar.getResult();
                    if (reduceResult != null){
                        List<ReduceCarList> list = reduceResult.getCarList();
                        List<ReduceDealer> datas = new ArrayList<>();
                        for (int i = 0; i < list.size(); i++) {
                            ReduceDealer data = list.get(i).getDealer();
                            datas.add(data);
                        }
                        seniorityAdapter.addData(list);
                        helper.addReduceCarlist(list, datas);
                    }

                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
                seniorityAdapter.addData(helper.seacherReduceCarlist());
            }
        }){
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String,String> map = new HashMap<String,String>();
                map.put("Accept-Encoding","");
                return map;
            }
        };
        volleySingleton.addRequest(stringRequest);

    }
}
